<?php
if (!defined("MCTDESIGN")) die("# ");
switch($act){
	case "category":
	   $title = getTitle(1);
	   $content = show_list_hotels();
       break;
   case "details":
	   $title = getTitle(2);
	   $content = show_hotel();
       break;   
   default : 
	   $content = msg_Error("<br><br><br>KHÔNG TÌM THẤY TRANG NÀY");
	   break;     
}
function getTitle($num){
    global $anyDB,$tb_fix;
    $tit = "";
    if($num == 1){
        $rootid = (int)$_GET['rootId'];
        $catid = (int)$_GET['catId'];
        $sql = "select hca_title from ".$tb_fix."_hotel_categorys where hca_id = '$catid'";
        $q = $anyDB->query($sql);
        $r = $anyDB->getNext($q);
        $tit = $r['hca_title']; 
    }
    else if($num == 2){
        $id = (int)$_GET['id'];
        $sql = "select hos_title from ".$tb_fix."_hotels where hos_id = '$id'";
        $q = $anyDB->query($sql);
        $r = $anyDB->getNext($q);
        $tit = $r['hos_title'];
    }
    if($tit == "") $tit = "Khách Sạn ";
    return $tit;
}

function show_hotel(){
    global $anyDB, $tb_fix, $temp, $dir_tpl;
    $rootId = (int)$_GET['rootId'];
    $catId = (int)$_GET['catId'];;
    $Id = (int)$_GET['id'];
    $temp->assign( 'base_tpl', "".$dir_tpl."" );
    // tour cat name
    get_parent_names($catId, $parent_name);
	$temp->assign( 'parent_name', "$parent_name" );
    $sql = "select * from ".$tb_fix."_hotels 
                where hos_id = '$Id'                   
                limit 0,1";
                
    $q = $anyDB->query($sql);
    $r = $anyDB->getNext();
    $hos_id = $r['hos_id'];
    // get images    
    $sql = "select * from ".$tb_fix."_hotel_images where him_hos_id = '$hos_id'";
    $q = $anyDB->query($sql);
    if($anyDB->numRows($q) > 0){
        $row = $anyDB->getAll();
        foreach($row as $key =>$val){
            $r['images'][] = $val;
        }    
    }
    $cat_id = $r['hos_hca_id'];
    $temp->assign("row",$r);
    //echo "<pre>";print_r($r);die;
    //TOUR CUNG LOAI 
    $sql = "select h.*,hc.hca_id,hc.hca_title from ".$tb_fix."_hotels as h
                left join ".$tb_fix."_hotel_categorys as hc
                    on h.hos_hca_id = hc.hca_id
                where
                    h.hos_id <> $hos_id and h.hos_hca_id = '$cat_id'
                limit 0,10   
    ";
    //echo $sql;
    $q = $anyDB->query($sql);
    $o_tour = $anyDB->getAll($q);
    foreach($o_tour as $key=>$val){
        $o_tour[$key]['hca_title'] = convertText($val['hca_title']);
        $o_tour[$key]['hosURL'] = convertText($val['hos_title']);
    }
    $temp->assign("other",$o_tour);
    $content = $temp->fetch( 'modules/_hotel_details.mct' );
    return $content;
}

function get_parent_names($cat_id, &$str) {

	global $mysql, $tb_fix, $stop, $dir_tpl;

	$query = $mysql->query("SELECT * FROM ".$tb_fix."_hotel_categorys WHERE hca_id='$cat_id' ORDER BY hca_id ASC");
    if($mysql->num_rows > 0){
        while ($result = $mysql->fetch_array($query)){

    		if ( $cat_id == $result['hca_id'] ){
    
    			get_parent_names($result['hca_parent_id'],  $str);
                $caturl = convertText($result['hca_title']);
    			$str .= ' <img src="'.$dir_tpl.'/images/di_toi.gif">  <a class="active" href="KhachSan/'.$result['hca_parent_id'].'/'.$caturl.'/'.$result['hca_id'].'/">'. $result['hca_title'] .'</a>';
    
    		}
    
    	}    
    }else{
        $str = '<a class="active" href="KhachSan/"> Khách Sạn</a>';
    }
	

}
function show_list_hotels(){
    global $anyDB, $tb_fix, $temp, $dir_tpl;
    $rootId = (int)$_GET['rootId'];
    $catId = (int)$_GET['catId'];
    $sale = fixquotes(stripslashes($_GET['act']));
	$temp->assign( 'base_tpl', "".$dir_tpl."" );
    $temp->assign( 'sale', "$sale" );
    // tour cat name
    get_parent_names($catId, $parent_name);
	$temp->assign( 'parent_name', "$parent_name" );
    // show Tour
 // phân trang
    $pagegroup_size = 10;
    $page=_POST('p');
    if (!$page||$page==0){$page=1;}
    $limit=_POST('limit');
    if($limit==''||$limit>30) {$limit=10;}
    $offset=($page-1)*$limit;
    $li="limit $offset,$limit";
    $where ="";
    $sqlSale = "";
    if($catId) {
        if($rootId == 0){
           $where .= " h.hos_hca_root_id = '$catId'"; 
        }else{
            $where .= " h.hos_hca_id = '$catId' and h.hos_hca_root_id = '$rootId'"; 
        }
          
    }else if($rootId){
        $where .= " h.hos_hca_root_id = '$rootId'";
    }
    else {
        $where.= " 1=1";   
    }
    if($sale != "" && $sale = "saleoff"){
        $sqlSale = " and h.hos_sale_off = 1";
    }   
    $sql = "select h.hos_id as tongso from ".$tb_fix."_hotels as h
                left join ".$tb_fix."_hotel_categorys as hc
                    on h.hos_hca_id = hc.hca_id
                left join ".$tb_fix."_hotel_images as him
                    on h.hos_id = him.him_hos_id
                where
                    $where
                    $sqlSale                     
                group by h.hos_title
                order by 
                    h.hos_id desc, him.him_id asc
                 ";
    $q = $anyDB->query($sql);
    $n = $anyDB->numRows($q);
    $totalRecord = $n; // tong so ban ghi
    $sql = "select h.*,hc.hca_id,hc.hca_title,him.him_pic from ".$tb_fix."_hotels as h
                left join ".$tb_fix."_hotel_categorys as hc
                    on h.hos_hca_id = hc.hca_id
                left join ".$tb_fix."_hotel_images as him
                    on h.hos_id = him.him_hos_id
                where
                    $where
                    $sqlSale                           
                group by h.hos_title
                order by 
                    h.hos_id desc, him.him_id asc
                $li            
    ";
    
    $q = $anyDB->query($sql);
    $r = $anyDB->getAll($q);
    //echo "<pre>";print_r($r);
    foreach($r as $k => $v){
        $r[$k]['catURL'] = convertText($v['hca_title']);
        $r[$k]['ksTitle'] =convertText($v['hos_title']);
        $r[$k]['ks_mota'] =subCutString($v['hos_desc'],20);  
        if(empty($v['him_pic'])){
           $r[$k]['him_pic'] = $dir_tpl.'/images/noImage.jpg'; 
        }
    }
    $temp->assign("rows",$r);    
    
    $getURL = getURLPaging();
    $pages = pagenavigator($page,$totalRecord,$limit,$pagegroup_size,"$getURL");
    $temp->assign('pages',$pages);
    $temp->assign('gthotel',get_data('shop_title_khachskm','config_values'));                    
	$content = $temp->fetch( 'modules/_hotels.mct' );
    return $content;
}
function getURLPaging(){
    $getURL = $_SERVER['REQUEST_URI'];
    $p = $_GET['p'];
    $newURL = str_replace("&p=$p","",$getURL);
    return $newURL;
}
?>